import { BrowserWindow, BrowserWindowConstructorOptions } from 'electron';
import path from 'path';

/**
 * 窗口基类
 *
 * @author chitanda
 * @date 2022-06-08 16:06:55
 * @export
 * @abstract
 * @class BaseWindow
 */
export abstract class BaseWindow extends BrowserWindow {
  /**
   * Creates an instance of AppWindow.
   *
   * @author chitanda
   * @date 2022-06-07 19:06:50
   * @param {BrowserWindowConstructorOptions} [options={}]
   */
  constructor(options: BrowserWindowConstructorOptions = {}) {
    super({
      // 默认不显示，等窗口初始化完毕再显示
      show: false,
      webPreferences: {
        contextIsolation: true,
        nodeIntegration: true,
        preload: path.join(__dirname, '../electron-preload/index.js'),
        webSecurity: false,
      },
      icon: path.join(__dirname, '../resources/logo.ico'),
      frame: true, // 标题栏和边框一并隐藏
      ...options,
    });
  }

  /**
   * 初始化
   *
   * @author chitanda
   * @date 2022-06-07 19:06:49
   * @protected
   */
  protected init(): void {
    this.on('resize', () => {
      this.webContents.send('resizeEvent');
    });

    this.once('ready-to-show', () => {
      this.show();
    });
  }
}
